let fetch = require("../../utils/fetch");
let app = getApp();
Page({
    data: {
        winHeight: "",//窗口高度
        currentTab: 0, //预设当前项的值
        imageUrl: app.globalData.imageUrl,
        allList: {
            isLast: false,
            curPage: 1,
            list: [],
            isLoading: false

        },
        unpayList: {
            isLast: false,
            curPage: 1,
            list: [],
            isLoading: false

        },
        succList: {
            isLast: false,
            curPage: 1,
            list: [],
            isLoading: false
        }
    },

    // 滚动切换标签样式
    switchTab: function (e) {
        console.log(e.detail.current)
        this.setData({
            currentTab: e.detail.current
        });
        this.judgeSwitch(e.detail.current)

        // this.checkCor();
    },
    judgeSwitch: function (current) {
        if (current == 0) {
            if (this.data.allList.list.length == 0) {
                this.loadMore(100, "allList")
            }
        } else if (current == 1) {
            if (this.data.unpayList.list.length == 0) {
                this.loadMore(0, "unpayList")
            }

        } else if (current == 2) {

            if (this.data.succList.list.length == 0) {
                this.loadMore(2, "succList")
            }

        }

    },
    // 点击标题切换当前页时改变样式
    switchNav: function (e) {
        var cur = e.currentTarget.dataset.current;
        console.log(e)
        if (this.data.currentTaB == cur) { return false; }
        else {
            this.setData({
                currentTab: cur
            })
        }
        this.judgeSwitch(cur)
    },
    //判断当前滚动超过一屏时，设置tab标题滚动条。
    checkCor: function () {
        if (this.data.currentTab > 4) {
            this.setData({
                scrollLeft: 300
            })
        } else {
            this.setData({
                scrollLeft: 0
            })
        }
    },
    onLoad: function () {
        wx.navigateToMiniProgram({
            appId:'wxbd687630cd02ce1d',
            path:'pages/index/index',
            extraData:{
                appid:'wx426a3015555a46be',
                contract_code:'122',
                contract_display_account:'张三',
                mch_id:'1223816102',
                notify_url:'https://www.qq.com/test/papay',
                plan_id:'106',
                request_serial:'123',
                timestamp:1414488825,
                sign:'FF1A406564EE701064450CA2149E2514'
            },
        success(res) {
            console.log(res)
        },
        fail(res) {
            // 未成功跳转到签约小程序 
            console.log(res)
        }
        })
        console.log('onLoad')
        var that = this;
        //  高度自适应
        wx.getSystemInfo({
            success: function (res) {
                var clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth,
                    rpxR = 750 / clientWidth;

                var calc = clientHeight * rpxR;
                console.log(clientHeight, clientWidth, rpxR, calc)
                console.log(calc)
                that.setData({
                    winHeight: calc
                });
            }
        });
        // 加载全部状态的订单
        this.onShowMethod(100, 1, 'allList')
    },
    onReachBottom: function () {
        console.log('onReachBottom')

    },
    lower: function (e) {
        console.log('lower', e.currentTarget.dataset.list)
        this.loadMore(e.currentTarget.dataset.status, e.currentTarget.dataset.list)

    },
    loadMore: function (orderStatus, lists) {
        if (this.data[lists].isLast||this.data[lists].isLoading) {
            return
        } else {
            this.getOrderList(orderStatus, this.data[lists].curPage, lists)
        }

    },
    onShowMethod: function (orderStatus, pageNum, lists) {
        fetch.showLoading({ title: '加载中' });
        wx.showNavigationBarLoading();
        this.getOrderList(orderStatus, pageNum, lists)

    },
    getOrderList: function (orderStatus, pageNum, lists) {
        this.setData({
            [lists]: {
                isLast: this.data[lists].isLast,
                curPage: this.data[lists].curPage,
                list: this.data[lists].list,
                isLoading: true
            },
        })
        fetch.getOrders(orderStatus, pageNum).then((response) => {
            const res = response.data;
            if (res.code == 200) {
                console.log(this.data[lists])
                let list = this.data[lists].list.concat(res.obj.result)
                this.setData({
                    [lists]: {
                        isLast: res.obj.lastPage,
                        curPage: this.data.allList.curPage + 1,
                        list: list,
                        isLoading: false
                    },

                })
            }
            fetch.hideLoading();
            wx.hideNavigationBarLoading()
            fetch.compatibilityHandler(wx.stopPullDownRefresh)


        }).catch((err) => {
            fetch.hideLoading()
            console.log(err)
            wx.hideNavigationBarLoading()
            fetch.compatibilityHandler(wx.stopPullDownRefresh)
        })
    }
})